{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 可变对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 每个对象都保存了三个数据：\n",
    "    1. id（标识）\n",
    "    2. type（类型）\n",
    "    3. value（值）\n",
    "- 列表就是一个可变对象\n",
    "- 两种情况\n",
    "    1. 改对象\n",
    "        - `列表[索引] = 字面量`\n",
    "        - 通过变量去修改**对象**的值\n",
    "        - 这种操作不会改变变量所指向的对象\n",
    "    2. 改变量\n",
    "        - `变量 = 字面量`\n",
    "        - 给**变量**重新赋值\n",
    "        - 这种操作会改变变量所指向的对象\n",
    "- 一般只有在为变量赋值时才是修改变量，其余的都是修改对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = [1, 2, 3], id = 140179764046656\n",
      "a = ['Hello', 2, 3], id = 140179764046656\n",
      "a = [4, 5, 6], id = 140179764614592\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')\n",
    "\n",
    "a[0] = 'Hello' # 改对象\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')\n",
    "\n",
    "a = [4,5,6] # 改变量\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 当修改对象时，如果有其它变量也指向了该对象，则修改也会体现在其他的变量中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = [1, 2, 3], id = 140179764614592\n",
      "a = [1, 2, 3], id = 140179764614592\n",
      "a = ['Hello World', 2, 3], id = 140179764614592\n",
      "b = ['Hello World', 2, 3], id = 140179764614592\n",
      "a = ['Hello World', 2, 3], id = 140179764614592\n",
      "b = Hello World, id = 140179754483120\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = a\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')\n",
    "print(f'a = {b}, id = {id(b)}')\n",
    "\n",
    "b[0] = 'Hello World'\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')\n",
    "print(f'b = {b}, id = {id(b)}')\n",
    "\n",
    "b = 'Hello World'\n",
    "\n",
    "print(f'a = {a}, id = {id(a)}')\n",
    "print(f'b = {b}, id = {id(b)}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.4 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}